<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-评价订单 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="component-modal-evaluate-order" @click.stop>
		<uni-popup ref="popupModal" type="bottom" :safe-area="false" @change="onChange">
			<view class="popup-box" :style="{'--theme-color': themeColor}">
				<view class="popup-header">
					<view class="title">评价</view>
					<image class="close" src="/static/close.png" mode="aspectFit" @click="onClose"></image>
				</view>
				<view class="popup-content">
					<scroll-view scroll-y class="contetnt-scroll">
						<view class="scroll-driver flex align-items-center">
							<image class="driver-avatar" :src="driverInfo.avatar" mode="aspectFill"></image>
							<view class="driver-info flex-item">
								<view class="info-name text-ellipsis">{{driverInfo.name}}</view>
								<view class="info-tag text-ellipsis">接单数量：{{driverInfo.acceptance_number}}单</view>
							</view>
						</view>
						<view class="scroll-form" v-if="orderEvaluateState == 2">
							<view class="form-status flex flex-between-center">
								<view class="status-item flex flex-center" :class="{active: evaluateForm.satisfaction_status == 1}" @click="changeStatus(1)">
									<view class="item-bg"></view>
									<image class="item-icon" src="/static/passenger/evaluate_1.png" mode="aspectFit"></image>
									<text class="item-text">满意</text>
								</view>
								<view class="status-item flex flex-center" :class="{active: evaluateForm.satisfaction_status == 2}" @click="changeStatus(2)">
									<view class="item-bg"></view>
									<image class="item-icon" src="/static/passenger/evaluate_2.png" mode="aspectFit"></image>
									<text class="item-text">不满意</text>
								</view>
							</view>
							<view class="form-option flex justify-content-between flex-wrap" v-if="evaluateForm.satisfaction_status == 1">
								<view class="option-item" :class="{active: evaluateForm.content.includes(item.id)}" v-for="item in evaluateOptions.satisfied" :key="item.id" @click="changeOption(item.id)">
									<view class="item-bg"></view>
									<text class="item-text">{{item.content}}</text>
								</view>
							</view>
							<view class="form-option flex justify-content-between flex-wrap" v-else>
								<view class="option-item" :class="{active: evaluateForm.content.includes(item.id)}" v-for="item in evaluateOptions.dissatisfied" :key="item.id" @click="changeOption(item.id)">
									<view class="item-bg"></view>
									<text class="item-text">{{item.content}}</text>
								</view>
							</view>
							<view class="form-textarea">
								<textarea class="textarea" v-model="evaluateForm.remarks" maxlength="50" placeholder="请输入其他信息" placeholder-class="placeholder" />
								<view class="number">{{evaluateForm.remarks.length}}/50</view>
							</view>
						</view>
						<view class="scroll-info" v-else>
							<view class="info-status flex justify-content-center">
								<view class="status-item flex flex-center" v-if="evaluateInfo.satisfaction_status == 2">
									<image class="item-icon" src="/static/passenger/evaluate_2.png" mode="aspectFit"></image>
									<text class="item-text">不满意</text>
								</view>
								<view class="status-item flex flex-center" v-else>
									<image class="item-icon" src="/static/passenger/evaluate_1.png" mode="aspectFit"></image>
									<text class="item-text">满意</text>
								</view>
							</view>
							<view class="info-option flex justify-content-between flex-wrap" v-if="evaluateInfo.content && evaluateInfo.content.length">
								<view class="option-item" v-for="(item, index) in evaluateInfo.content" :key="index">
									<view class="item-bg"></view>
									<text class="item-text">{{item}}</text>
								</view>
							</view>
							<view class="info-textarea" v-if="evaluateInfo.remarks">{{evaluateInfo.remarks}}</view>
						</view>
					</scroll-view>
				</view>
				<view class="popup-btn" v-if="orderEvaluateState == 2">
					<view class="btn" @click="onConfirm">匿名评价</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		name: "componentModalEvaluateOrder",
		data() {
			return {
				// 司机信息
				driverInfo: {},
				// 订单id
				orderId: null,
				// 订单评价状态
				orderEvaluateState: 2,
				// 评价选项
				evaluateOptions: {
					satisfied: [],
					dissatisfied: [],
				},
				// 评价表单
				evaluateForm: {
					satisfaction_status: 1,
					content: [],
					remarks: "",
				},
				// 已评价内容
				evaluateInfo: {
					satisfaction_status: 1,
					content: [],
					remarks: "",
				},
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		mounted() {
			this.getEvaluateOption()
		},
		methods: {
			// 打开模态框
			open(data) {
				this.orderId = data.id
				this.orderEvaluateState = data.state
				this.evaluateForm = {
					satisfaction_status: 1,
					content: [],
					remarks: "",
				}
				if (this.orderEvaluateState == 1) {
					this.driverInfo = data.driverInfo || {}
					uni.showLoading({
						title: "加载中",
						mask: true
					})
					this.getEvaluateDetails(() => {
						uni.hideLoading()
						this.$refs.popupModal.open()
					})
				} else {
					if (data.driverInfo) {
						this.driverInfo = data.driverInfo
						this.$refs.popupModal.open()
					} else {
						uni.showLoading({
							title: "加载中",
							mask: true
						})
						this.getOrderInfo(() => {
							uni.hideLoading()
							this.$refs.popupModal.open()
						})
					}
				}
			},
			// 关闭弹窗
			onClose() {
				this.$refs.popupModal.close()
			},
			// 改变事件
			onChange(e) {
				this.$emit("onChange", e.show)
			},
			// 获取评价选项
			getEvaluateOption(fn) {
				this.$util.request("passenger.order.evaluateOptions").then(res => {
					if (res.code == 1) {
						this.evaluateOptions = res.data || {}
						if (fn) fn()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取评价选项 ', error)
				})
			},
			// 获取订单详情
			getOrderInfo(fn) {
				this.$util.request("passenger.order.details", {
					id: this.orderId,
				}).then(res => {
					if (res.code == 1) {
						this.driverInfo = res.data.driver_info
						if (fn) fn()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取订单详情 ', error)
				})
			},
			// 匿名评价
			onConfirm() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				var data = {
					order_id: this.orderId,
					satisfaction_status: this.evaluateForm.satisfaction_status,
					remarks: this.evaluateForm.remarks
				}
				if (this.evaluateForm.content.length) data.options_ids = this.evaluateForm.content.join()
				this.$util.request("passenger.order.evaluate", data).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						uni.showToast({
							icon: "success",
							title: "匿名评价成功"
						})
						this.onClose()
						this.$emit("onUpdate")
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('匿名评价 ', error)
				})
			},
			// 改变评价状态
			changeStatus(id) {
				this.evaluateForm.content = []
				this.evaluateForm.satisfaction_status = id
			},
			// 改变评价选项
			changeOption(id) {
				let index = this.evaluateForm.content.findIndex(item => item == id)
				if (index > -1) {
					delete this.evaluateForm.content[index]
					this.$forceUpdate()
				} else this.evaluateForm.content.push(id)
			},
			// 获取评价详情
			getEvaluateDetails(fn) {
				this.$util.request("passenger.order.evaluateDetails", {
					order_id: this.orderId
				}).then(res => {
					if (res.code == 1) {
						this.evaluateInfo = res.data
						if (fn) fn()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取评价详情 ', error)
				})
			},

		},
	}
</script>

<style lang="scss">
	.component-modal-evaluate-order {
		position: relative;
		z-index: 999;

		.popup-box {
			background: #FFFFFF;
			border-radius: 32rpx 32rpx 0 0;
			padding-top: 32rpx;
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.popup-header {
				padding: 0 48rpx;
				position: relative;

				.title {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 48rpx;
					text-align: center;
				}

				.close {
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					top: 0;
					right: 48rpx;
				}
			}

			.popup-content {
				padding: 12rpx 32rpx 32rpx;

				.contetnt-scroll {
					overflow: hidden;
					max-height: 75vh;
					min-height: 25vh;

					.scroll-driver {
						margin-top: 20rpx;
						border-radius: 32rpx;
						background: #FFF;
						padding: 32rpx;
						border-bottom: 1px solid #EBEBEB;

						.driver-avatar {
							margin-right: 16rpx;
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
						}

						.driver-info {
							.info-name {
								color: #242629;
								font-size: 32rpx;
								font-weight: 600;
								line-height: 44rpx;
							}

							.info-tag {
								margin-top: 2rpx;
								color: #8D929C;
								font-size: 24rpx;
								font-weight: 600;
								line-height: 34rpx;
							}
						}
					}

					.scroll-form {
						.form-status {
							margin-top: 40rpx;

							.status-item {
								border-radius: 32rpx;
								overflow: hidden;
								border: 1px solid #EBEBEB;
								background: #FFF;
								padding: 14rpx;
								width: 48%;
								position: relative;
								z-index: 1;

								.item-bg {
									position: absolute;
									top: 0;
									left: 0;
									right: 0;
									bottom: 0;
									background: var(--theme-color);
									opacity: 0;
									z-index: -1;
								}

								.item-icon {
									width: 48rpx;
									height: 48rpx;
								}

								.item-text {
									color: #242629;
									font-size: 28rpx;
									font-weight: 600;
									line-height: 40rpx;
									margin-left: 16rpx;
								}

								&.active {
									border-color: var(--theme-color);

									.item-bg {
										opacity: .05;
									}

									.item-text {
										color: var(--theme-color);
									}
								}
							}
						}

						.form-option {
							padding-top: 16rpx;

							.option-item {
								min-width: 48%;
								margin-top: 32rpx;
								padding: 14rpx 30rpx;
								border-radius: 16rpx;
								border: 1px solid #8D929C;
								text-align: center;
								position: relative;
								z-index: 1;

								.item-bg {
									position: absolute;
									top: 0;
									left: 0;
									right: 0;
									bottom: 0;
									background: var(--theme-color);
									opacity: 0;
									z-index: -1;
								}

								.item-text {
									color: #8D929C;
									font-size: 24rpx;
									font-weight: 600;
									line-height: 34rpx;
								}

								&.active {
									border-color: var(--theme-color);

									.item-bg {
										opacity: .05;
									}

									.item-text {
										color: var(--theme-color);
									}
								}
							}
						}

						.form-textarea {
							margin-top: 32rpx;
							position: relative;
							padding: 22rpx 30rpx 52rpx;
							height: 176rpx;
							border: 1px solid #EBEBEB;
							border-radius: 32rpx;

							.textarea {
								width: 100%;
								height: 100%;
								color: #242629;
								font-size: 24rpx;
								font-weight: 600;
								line-height: 34rpx;
								box-sizing: border-box;
							}

							.placeholder {
								color: #8D929C;
								font-size: 24rpx;
								font-weight: 600;
							}

							.number {
								position: absolute;
								right: 16rpx;
								bottom: 16rpx;
								color: #8D929C;
								font-size: 24rpx;
								line-height: 34rpx;
							}
						}
					}

					.scroll-info {
						.info-status {
							margin-top: 40rpx;

							.status-item {
								.item-icon {
									width: 48rpx;
									height: 48rpx;
								}

								.item-text {
									color: #242629;
									font-size: 28rpx;
									font-weight: 600;
									line-height: 40rpx;
									margin-left: 16rpx;
								}
							}
						}

						.info-option {
							padding-top: 16rpx;

							.option-item {
								min-width: 48%;
								margin-top: 32rpx;
								padding: 14rpx 30rpx;
								border-radius: 16rpx;
								border: 1px solid var(--theme-color);
								text-align: center;
								position: relative;
								z-index: 1;

								.item-bg {
									position: absolute;
									top: 0;
									left: 0;
									right: 0;
									bottom: 0;
									background: var(--theme-color);
									opacity: 0.05;
									z-index: -1;
								}

								.item-text {
									color: var(--theme-color);
									font-size: 24rpx;
									font-weight: 600;
									line-height: 34rpx;
								}
							}
						}

						.info-textarea {
							margin-top: 48rpx;
							color: #242629;
							font-size: 28rpx;
							font-weight: 600;
							line-height: 40rpx;
						}
					}
				}
			}

			.popup-btn {
				padding: 16rpx 48rpx;

				.btn {
					color: #FFFFFF;
					font-size: 28rpx;
					line-height: 40rpx;
					text-align: center;
					padding: 28rpx;
					border-radius: 48rpx;
					background: var(--theme-color);
				}
			}
		}
	}
</style>